package com.michael.study.jetpack.network

import okhttp3.*
import okhttp3.logging.HttpLoggingInterceptor
import java.io.IOException
import java.security.KeyManagementException
import java.security.NoSuchAlgorithmException
import java.security.SecureRandom
import java.security.cert.CertificateException
import java.security.cert.X509Certificate
import java.util.concurrent.TimeUnit
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSession
import javax.net.ssl.X509TrustManager

/**
 * Description:
 *
 * Created by liuguoquan on 2020/3/19 16:32.
 */
class HttpClient {

    private class TrustAnyTrustManager : X509TrustManager {
        @Throws(CertificateException::class)
        override fun checkClientTrusted(
            chain: Array<X509Certificate>,
            authType: String
        ) {
        }

        @Throws(CertificateException::class)
        override fun checkServerTrusted(
            chain: Array<X509Certificate>,
            authType: String
        ) {
        }

        override fun getAcceptedIssuers(): Array<X509Certificate> {
            return arrayOf()
        }
    }

    private class TrustAnyHostnameVerifier : HostnameVerifier {
        override fun verify(
            hostname: String,
            session: SSLSession
        ): Boolean {
            return true
        }
    }

    companion object {

        fun getDefaultHttpClient(): OkHttpClient {
            val builder = OkHttpClient.Builder()
            builder.connectTimeout(45, TimeUnit.SECONDS)
            builder.writeTimeout(45, TimeUnit.SECONDS)
            builder.readTimeout(45, TimeUnit.SECONDS)
            builder.connectionPool(ConnectionPool(5, 1, TimeUnit.MINUTES))
            builder.interceptors()
                .add(
                    object : Interceptor {
                        @Throws(IOException::class)
                        override fun intercept(chain: Interceptor.Chain): Response {
                            val b: Request.Builder = chain.request().newBuilder();
                            b.addHeader("token","2762be8fee1bf6f319878fecb4363f7f");
                            return chain.proceed(b.build())
                        }
                    })
            val loggingInterceptor = HttpLoggingInterceptor()
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
            builder.interceptors().add(loggingInterceptor)

            try {
                val sc = SSLContext.getInstance("SSL")
                sc.init(
                    null,
                    arrayOf(TrustAnyTrustManager()),
                    SecureRandom()
                )
                builder.sslSocketFactory(sc.socketFactory, TrustAnyTrustManager())
                builder.hostnameVerifier(TrustAnyHostnameVerifier())
            } catch (ignored: KeyManagementException) {
            } catch (ignored: NoSuchAlgorithmException) {
            }
            return builder.build()
        }
    }
}